local scheduler = cc.Director:getInstance():getScheduler()


function s_createBBbyType(itype)
    if itype >= pp.bt_comStart and itype <= pp.bt_comEnd then
        return Bubble.createWithType(itype)
    elseif itype >= pp.bt_paintStart and itype <= pp.bt_paintEnd then
        return BubblePaint.createWithType(itype)
    elseif itype == pp.bt_key then
        return BubbleKey.create()
    elseif itype == pp.bt_mixed then
        return BubbleColorful.create()
    elseif itype == pp.bt_boom then
        return BubbleBoom.create()
    elseif itype == pp.bt_coin then
        return BubbleCoin.create()
    elseif itype == pp.bt_space then
        return BubbleSpace.create()
    elseif itype == pp.bt_wood then
        return BubbleWood.create()
    elseif itype == pp.bt_up then
        return BubbleUp.create()
    elseif itype == pp.bt_down then
        return BubbleDown.create()
    elseif itype == pp.bt_puzzle then
        return BubblePuzzle.create()
    elseif itype == pp.bt_boss then
        return BubbleBoss.create()
    else
        return nil
    end
end
   
-------------------------------------------------------
local bubbleImages = {
    [1] = "bubbles/red.png",
    [2] = "bubbles/orange.png",
    [3] = "bubbles/yellow.png",
    [4] = "bubbles/green.png",
    [5] = "bubbles/blue.png",
    [6] = "bubbles/cyan.png",
    [7] = "bubbles/purple.png",
     
    [1 + pp.bt_paintOffset] = "bubbles/red-c.png",
    [2 + pp.bt_paintOffset] = "bubbles/orange-c.png",
    [3 + pp.bt_paintOffset] = "bubbles/yellow-c.png",
    [4 + pp.bt_paintOffset] = "bubbles/green-c.png",
    [5 + pp.bt_paintOffset] = "bubbles/blue-c.png",
    [6 + pp.bt_paintOffset] = "bubbles/cyan-c.png",
    [7 + pp.bt_paintOffset] = "bubbles/purple-c.png",
    
    [pp.bt_mixed] = "bubbles/colorful.png",
    [pp.bt_key] = "bubbles/key.png",
    [pp.bt_boom] = "bubbles/boom.png",
    [pp.bt_coin] = "bubbles/coin.png",
    [pp.bt_space] = "bubbles/shuipao.png",
    [pp.bt_wood] = "bubbles/wood.png",
    [pp.bt_up] = "bubbles/shangtui.png",
    [pp.bt_down] = "bubbles/xiatui.png",
    [pp.bt_puzzle] = "bubbles/puzzle.png",
    [pp.bt_fenji] = "bubbles/fenji.png",
    [pp.bt_yuheng] = "bubbles/yuheng.png",
}
    
Bubble = class("Bubble")

function Bubble:ctor()
    self.row = 0
    self.col = 0
    self.passable = false
end

function Bubble.createWithType(itype)
    if itype < 1 or itype > 7 then
    	assert(1, "bubble create itype invalid")
    	return nil  
    end
    local bubble = Bubble.new()
    bubble.itype = itype
    bubble:init()

    return bubble
end

function Bubble:init()
    self.view = 	ccui.ImageView:create(bubbleImages[self.itype], ccui.TextureResType.plistType)
    self.view:setAnchorPoint(cc.p(0.5,0.5)) 
end

function Bubble:newView()
    --d_print("new view:"..bubbleImages[self.itype])
    self.view = ccui.ImageView:create(bubbleImages[self.itype], ccui.TextureResType.plistType)
end

function Bubble:changeToNewType(it)
--    local uiinfo = UIInfo.inst
--    uiinfo:readInfoFromNode(self.view)
--    local parent = self.view:getParent()
--    self.view:removeFromParent(true)
--
    self.itype = it
--    self:newView()
--    uiinfo:setInfoToNode(self.view)
--    parent:addChild(self.view)
    self.view:loadTexture(bubbleImages[self.itype], ccui.TextureResType.plistType)
    uiEffect.bounceIn(self.view)
end

---------------------------------------------------------------------------
BubbleKey = class("BubbleKey", Bubble)

function BubbleKey.create()
    local bubble = BubbleKey.new()
    bubble.itype = pp.bt_key
    bubble:init()
    return bubble
end

---------------------------------------------------------------------------
BubbleCoin = class("BubbleCoin", Bubble)

function BubbleCoin.create()
    local bubble = BubbleCoin.new()
    bubble.itype = pp.bt_coin
    bubble:init()
    bubble.passable = true
    return bubble
end

---------------------------------------------------------------------------
BubbleSpace = class("BubbleSpace", Bubble)

function BubbleSpace.create()
    local bubble = BubbleSpace.new()
    bubble.itype = pp.bt_space
    bubble:init()
    bubble.passable = true
    return bubble
end

---------------------------------------------------------------------------
BubbleColorful = class("BubbleColorful", Bubble)

function BubbleColorful.create()
    local bubble = BubbleColorful.new()
    bubble.itype = pp.bt_mixed
    bubble:init()
    return bubble
end
---------------------------------------------------------------------------

BubbleBoom = class("BubbleBoom", Bubble)

function BubbleBoom.create()
    local bubble = BubbleBoom.new()
    bubble.itype = pp.bt_boom
    bubble:init()
    return bubble
end

---------------------------------------------------------------------------

BubblePaint = class("BubblePaint", Bubble)

function BubblePaint.createWithType(itype)
    if IS_DEBUG and itype < pp.bt_paintStart or itype > pp.bt_paintEnd then
        assert(1, "bubblePaint create itype invalid")
        return nil  
    end
    local bubble = BubblePaint.new()
    bubble.itype = itype
    bubble:init()
    return bubble
end

---------------------------------------------------------------------------

BubbleWood = class("BubbleWood", Bubble)

function BubbleWood.create()
    local bubble = BubbleWood.new()
    bubble.itype = pp.bt_wood
    bubble:init()
    return bubble
end

---------------------------------------------------------------------------

BubbleUp = class("BubbleUp", Bubble)

function BubbleUp.create()
    local bubble = BubbleUp.new()
    bubble.itype = pp.bt_up
    bubble:init()
    return bubble
end

---------------------------------------------------------------------------

BubbleDown = class("BubbleDown", Bubble)

function BubbleDown.create()
    local bubble = BubbleDown.new()
    bubble.itype = pp.bt_down
    bubble:init()
    return bubble
end

---------------------------------------------------------------------------

BubblePuzzle = class("BubblePuzzle", Bubble)

function BubblePuzzle.create()
    local bubble = BubblePuzzle.new()
    bubble.itype = pp.bt_puzzle
    bubble:init()
    return bubble
end

---------------------------------------------------------------------------

BubbleFenji = class("BubbleFenji", Bubble)

function BubbleFenji.create()
    local bubble = BubbleFenji.new()
    bubble.itype = pp.bt_fenji
    bubble:init()
    return bubble
end
---------------------------------------------------------------------------

BubbleYuheng = class("BubbleYuheng", Bubble)

function BubbleYuheng.create()
    local bubble = BubbleYuheng.new()
    bubble.itype = pp.bt_yuheng
    bubble:init()
    return bubble
end


---------------------------------------------------------------------------

BubbleBoss = class("BubbleBoss", Bubble)

function BubbleBoss.create()
    local bubble = BubbleBoss.new()
    bubble.itype = pp.bt_boss
    
    local boss = gameData.leveldata.boss
        
    bubble.view = ccui.ImageView:create(boss.image, ccui.TextureResType.plistType)
    bubble.view:setAnchorPoint(cc.p(0.5,0.5)) 

    bubble.curLife = boss.life
    bubble.curXpPercent = 0
    bubble.xpSpeed = 1 / boss.xp_interval
    bubble.scene = gameUtil.curLuaScene

    return bubble
end

function BubbleBoss:startAttack()
    local leveldata = gameData.leveldata
    local boss = leveldata.boss

    local function update(delta)
        self:update(delta)
    end
    self.view:scheduleUpdateWithPriorityLua(update, 0)
end

function BubbleBoss:pauseAttack()
    self.view:unscheduleUpdate()
end

function BubbleBoss:update(delta)
    local runPercentThisFrame = self.xpSpeed * delta
    self.curXpPercent = self.curXpPercent + runPercentThisFrame
    
    local function attack()
        --todo
        local bullets = self:genBullets()
        self.scene:boss_xpAttack(self, bullets)
    end  

    if self.curXpPercent >= 1 then
        attack()
        self.curXpPercent = 0
    end

    self.scene:boss_xpRefresh(self.curXpPercent)
end

function BubbleBoss:resumeAttack()
    local function update(delta)
        self:update(delta)
    end
    self.view:scheduleUpdateWithPriorityLua(update, 0)
end

function BubbleBoss:xpRollback(combIdx)
    local boss = gameData.leveldata.boss

    -- self:pauseAttack()
    -- local function resume()
    --     self:resumeAttack()
    -- end
    -- self.schedulePauseAttack = scheduler:scheduleScriptFunc(resume, boss.xp_rollback[combIdx], false)
    if combIdx > #boss.xp_rollback then
        combIdx = #boss.xp_rollback
    end
    self.curXpPercent = self.curXpPercent - boss.xp_rollback[combIdx]
    if self.curXpPercent < 0  then self.curXpPercent = 0 end

    self.scene:boss_xpRefresh(self.curXpPercent)
end

function BubbleBoss:genBullets()
    local bullets = {}
    local leveldata = gameData.leveldata
    local boss = leveldata.boss

    local function genBullet()
        local xpclass = boss.xp_class

        local idx = math.random(#xpclass)
        return xpclass[idx]
    end

    assert(boss.xp_min <= boss.xp_max, "")
    local btcnt = math.random(boss.xp_min, boss.xp_max)
    for i = 1,btcnt do
        bullets[#bullets+1] = genBullet()
    end

    return bullets
end

function BubbleBoss:onHit()
    self.curLife = self.curLife - 1
    self.scene:boss_lifeRefresh(self.curLife)

    if self.curLife == 0 then
        --dead
        self:pauseAttack()
        self.scene:boss_die()
    else
        self.scene:boss_genProtection()
    end
end

